/**
 * 
 */
package com.wsd.util;

import java.util.Iterator;
import java.util.Set;

import com.wsd.bean.Category;


/**
 * @author 小e
 *
 * 2010-3-10 下午04:56:02
 */
public class TreeUtil {
	private static String treeJSON;
	private static String checkTreeJSON;
	private static String checked = "";
	private synchronized static String createTree(Category node){
		StringBuffer str = new StringBuffer("{id:'" + node.getCId()+ "',text:'" + node.getCName() + "',level:'" + node.getCLevel() + "'" + checked);
		if(node.getCategories() == null || node.getCategories().size() == 0){
			str.append(",leaf:true");
			str.append("}");
		}else{
			str.append(",children:[");
			boolean first = true;
			for (Iterator iterator = node.getCategories().iterator(); iterator.hasNext();) {
				Category childNode = (Category) iterator.next();
				if(!first){
					str.append(",");
				}
				first = false;
				str.append(createTree(childNode));
				
			}
			str.append("]}");
		}
		
		return str.toString();
	}
	
	public static String createTreeJSON(Category root,boolean check){
		if(check){
			checked = ",checked:false";
		}else{
			checked = "";
		}
		return "[" + createTree(root) + "]";
	}
	
	/*public static String getAllGdbClassTreeJSON(){
		if(treeJSON == null){
			treeJSON = GdbClassUtil.getGdbClassTree(GdbClassUtil.getAllClass(),false);
		}
		return treeJSON;
	}*/
	
	/*public static String getAllGdbClassCheckTreeJSON(Set<Integer> filter){
		if(checkTreeJSON == null){
			checkTreeJSON = GdbClassUtil.getGdbClassTree(GdbClassUtil.getAllClass(filter),true);
		}
		return checkTreeJSON;
	}*/
	/**
	 * 得到所有的叶子节点
	 * @return
	 */
	/*public static int getLeafNodeCount(){
		return GdbClassUtil.getLeafClass().size();
	}*/

	/**
	 * @param filter
	 * @return
	 */
	/*public static Object getLeafNodeCount(Set<Integer> filter) {
		int count = 0;
		for (Integer integer : filter) {
			GdbClass gdbClass = GdbClassUtil.getGdbClass(integer);
			if(gdbClass.is_is_leaf()){
				count++;
			}
			
		}
		return getLeafNodeCount() - count;
	}*/

}
